/* 首页路由模块 */
const express = require('express');
const admin = express.Router();

// /login 是在app.js中app.use('/admin')后面添加的， admin/login
admin.post('/login', require('./admin/login'))
// 获取用户
admin.get('/user', require('./admin/user/list'))
// 添加用户
admin.post('/user', require('./admin/user/add'))
// 删除用户
admin.delete('/user', require('./admin/user/delete'))
// 修改用户状态
admin.put('/user/state-edit', require('./admin/user/state-edit'))
// 获取角色类型
admin.get('/user/role-type', require('./admin/user/role-type'))

// 添加语录
admin.get('/mood', require('./admin/mood/list'))
admin.post('/mood', require('./admin/mood/add'))
admin.put('/mood', require('./admin/mood/edit'))
admin.delete('/mood', require('./admin/mood/delete'))

// 分类
admin.get('/category', require('./admin/category/list'))
admin.post('/category', require('./admin/category/add'))
admin.put('/category', require('./admin/category/edit'))
admin.delete('/category', require('./admin/category/delete'))

// 文章
admin.get('/article', require('./admin/article/list'))
admin.post('/article', require('./admin/article/add'))
admin.put('/article', require('./admin/article/edit'))
admin.delete('/article', require('./admin/article/delete'))
admin.get('/article/:id', require('./admin/article/info'))

// 权限管理
admin.get('/page', require('./admin/rights/page-list'))
admin.post('/page', require('./admin/rights/page-add'))
admin.put('/page', require('./admin/rights/page-edit'))
admin.delete('/page', require('./admin/rights/page-delete'))
admin.get('/role', require('./admin/rights/role-list'))
admin.put('/role', require('./admin/rights/role-edit'))

// 百宝箱
admin.get('/collection/category', require('./admin/collection/category-list'))
admin.post('/collection/category', require('./admin/collection/category-add'))
admin.put('/collection/category', require('./admin/collection/category-edit'))
admin.delete('/collection/category', require('./admin/collection/category-delete'))

admin.get('/collection', require('./admin/collection/list'))
admin.post('/collection', require('./admin/collection/add'))
admin.put('/collection', require('./admin/collection/edit'))
admin.delete('/collection', require('./admin/collection/delete'))

// 轮播图
admin.get('/banner', require('./admin/banner/list'))
admin.post('/banner', require('./admin/banner/add'))
admin.put('/banner', require('./admin/banner/edit'))
admin.delete('/banner', require('./admin/banner/delete'))

// 数据统计
admin.get('/echarts/article', require('./admin/echarts/article'))
admin.get('/echarts/collection', require('./admin/echarts/collection'))

// 留言板
admin.get('/guestbook', require('./admin/guestbook/list'))
admin.delete('/guestbook', require('./admin/guestbook/delete'))

// 上传图片
const multer = require('multer')
const path = require("path")

var storage = multer.diskStorage({
    // 配置文件上传后存储的路径
    destination: function(req, file, cb) {
        // NodeJS的两个全局变量
        // console.log(__dirname);  //获取当前文件在服务器上的完整目录 
        // console.log(__filename); //获取当前文件在服务器上的完整路径 
        cb(null, path.join(__dirname, '../public/upload'))
    },
    // 配置文件上传后存储的路径和文件名
    filename: function(req, file, cb) {
        // cb(null, Date.now() + path.extname(file.originalname))
        cb(null, Date.now() + file.originalname)
    }
})
var upload = multer({
    storage: storage
})
admin.post('/profile', upload.single('avatar'), function(req, res, next) {
    const paths = req.file.path.split('\\')
    const len = paths.length
    res.json({
        path: '/' + paths[len - 2] + '/' + paths[len - 1],
    })
})
admin.post('/upload', upload.single('wangeditor-uploaded-image'), function(req, res, next) {
    const paths = req.file.path.split('\\')
    const len = paths.length
    res.json({
        "errno": 0, // 注意：值是数字，不能是字符串
        "data": {
            "url": '/' + paths[len - 2] + '/' + paths[len - 1], // 图片 src ，必须
        }
    })
    // 上传失败的返回格式：
    // res.json({
    //     "errno": 1, // 只要不等于 0 就行
    //     "message": "失败信息"
    // })
})

module.exports = admin;
